package com.os.task;

import cn.hutool.core.date.DateUtil;
import com.os.data.DataStore;
import com.os.model.DataNodeInfo;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;

import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 风亦未止
 * @date 2023/5/6 15:45
 */
@Log4j2
public class RemoveExpiredDataServer implements Runnable{

    @SneakyThrows
    @Override
    public void run() {
        while (true){
            Thread.sleep(5000);
            ConcurrentHashMap<String, DataNodeInfo> map =  DataStore.dataNodeStore;
            Iterator<Map.Entry<String, DataNodeInfo>> iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                Date date = DateUtil.parse(map.get(iterator.next().getKey()).getTime());
                //删除超过10秒的数据节点
                if(DateUtil.offsetSecond(date,10).isBefore(new Date())){
                    iterator.remove();
                }
            }
            log.info(map);
        }
    }
}
